package com.zzy.base.utils;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class ClipUtil {
    public interface Function {
        /** Invokes the function. */
        void invoke(String text);
    }

    void getClipBoardText(@Nullable Activity activity, final Function f) {
        if(Build.VERSION.SDK_INT >= 29 && activity != null){
            getTextFromClipFroAndroidQ(activity, f);
        }else{
            f.invoke(getTextFromClip(activity));
        }

//
    }

    /**
     * AndroidQ 获取剪贴板的内容
     */
    private void getTextFromClipFroAndroidQ(@NonNull final Activity activity, final Function f) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                ClipboardManager clipboardManager =
                        (ClipboardManager)activity.getSystemService(Context.CLIPBOARD_SERVICE);
                if (null == clipboardManager || !clipboardManager.hasPrimaryClip()) {
                    f.invoke("");
                    return;
                }
                ClipData clipData = clipboardManager.getPrimaryClip();
                if (null == clipData || clipData.getItemCount() < 1) {
                    f.invoke("");
                    return;
                }
                ClipData.Item item = clipData.getItemAt(0);
                if (item == null) {
                    f.invoke("");
                    return;
                }
                CharSequence clipText = item.getText();
                if (TextUtils.isEmpty(clipText))
                    f.invoke("");
                else
                    f.invoke(clipText.toString());
            }
        };
//        activity.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
//            @Override
//            public void onActivityCreated(@NonNull Activity activity, @androidx.annotation.Nullable Bundle savedInstanceState) {
//
//            }
//
//            @Override
//            public void onActivityStarted(@NonNull Activity activity) {
//
//            }
//
//            @Override
//            public void onActivityResumed(@NonNull Activity activity) {
//
//            }
//
//            @Override
//            public void onActivityPaused(@NonNull Activity activity) {
//
//            }
//
//            @Override
//            public void onActivityStopped(@NonNull Activity activity) {
//
//            }
//
//            @Override
//            public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {
//
//            }
//
//            @Override
//            public void onActivityDestroyed(@NonNull Activity activity) {
//                activity.getWindow().getDecorView().removeCallbacks(runnable);
//            }
//        });
        activity.getWindow().getDecorView().post(runnable);
    }

    private String getTextFromClip(Context context) {
        ClipboardManager clipboardManager =
                (ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);
        if (null == clipboardManager || !clipboardManager.hasPrimaryClip()) {
            return "";
        }
        ClipData clipData = clipboardManager.getPrimaryClip();
        if (null == clipData || clipData.getItemCount() < 1) {
            return "";
        }
        ClipData.Item item = clipData.getItemAt(0);
        if (item == null)
            return "";
        CharSequence clipText = item.getText();
        if (TextUtils.isEmpty(clipText))
            return "";
        else
            return clipText.toString();

    }


}
